<template>
    <div class="roleEditor">
        <el-dialog :visible="dialogFormVisible" width="30%" :show-close="false">
            <el-form :model="form" :rules="rules" ref="form">
                <el-form-item label="请输入密码" :label-width="formLabelWidth" prop="oldPassword">
                    <el-input v-model="form.oldPassword" autocomplete="off" show-password
                              placeholder="输入密码"></el-input>
                </el-form-item>
                <el-form-item label="请输入新密码" :label-width="formLabelWidth" prop="newPassword">
                    <el-input v-model="form.newPassword" autocomplete="off" show-password
                              placeholder="输入新密码"></el-input>
                </el-form-item>
                <el-form-item label="再次输入新密码" :label-width="formLabelWidth" prop="affirmedNewPassword">
                    <el-input v-model="form.affirmedNewPassword" autocomplete="off" show-password
                              placeholder="再次输入新密码"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitModifyPassword()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {systemApi,commonApi} from "../../api/api";

    export default {
        data() {
            return {
                form: {
                    oldPassword: null,
                    newPassword: null,
                    affirmedNewPassword: null,
                },
                formLabelWidth: '130px',
                rules: {
                    oldPassword: {required: true, trigger: 'blur'},
                    newPassword: {required: true, trigger: 'blur'},
                    affirmedNewPassword: {required: true, validator: this.validatePass, trigger: 'blur'}
                },
                dialogFormVisible: false
            };
        },
        methods: {
            submitModifyRole() {
                this.$refs["form"].validate((valid) => {
                    if (valid) {
                        systemApi.updateRole(this.form).then((res) => {
                                this.$message({message: res.data.msg, type: 'success'})
                                this.$emit('refreshRow', this.index, this.form)
                            }, commonApi.popoverErrorMessage
                        ).catch(commonApi.popoverErrorMessage);

                        this.dialogFormVisible = false;
                    }
                })

            },
            submitModifyPassword() {
                this.$refs["form"].validate((valid) => {
                        if (valid) {
                            systemApi.modifyPassword(this.form).then(res => {
                                    this.$message({message: res.data.msg, type: 'success'})
                                    this.$store.commit("removeUser");
                                    this.$alert('密码修改成功', '请重新登陆', {
                                        confirmButtonText: '确定',
                                        callback: () => {
                                            this.$router.push("/login");
                                        }
                                    })
                                },
                                commonApi.popoverErrorMessage).catch(commonApi.popoverErrorMessage)
                            this.dialogFormVisible = false;
                        }
                    }
                )
            },
            validatePass(rule, value, callback) {
                if (value !== this.form.newPassword) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            },
            setData(key, value) {
                this[key] = value
                return this
            }
        }
    };
</script>